<?php

use yii\db\Migration;

/**
 * Handles adding columns to table `{{%library_book}}`.
 * Has foreign keys to the tables:
 *
 * - `{{%library_collection}}`
 */
class m230515_120326_add_collection_id_column_to_library_book_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->addColumn('{{%library_book}}', 'collection_id', $this->integer());

        // creates index for column `collection_id`
        $this->createIndex(
            '{{%idx-library_book-collection_id}}',
            '{{%library_book}}',
            'collection_id'
        );

        // add foreign key for table `{{%library_collection}}`
        $this->addForeignKey(
            '{{%fk-library_book-collection_id}}',
            '{{%library_book}}',
            'collection_id',
            '{{%library_collection}}',
            'id',
            'SET NULL'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        // drops foreign key for table `{{%library_collection}}`
        $this->dropForeignKey(
            '{{%fk-library_book-collection_id}}',
            '{{%library_book}}'
        );

        // drops index for column `collection_id`
        $this->dropIndex(
            '{{%idx-library_book-collection_id}}',
            '{{%library_book}}'
        );

        $this->dropColumn('{{%library_book}}', 'collection_id');
    }
}
